home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / tcpdump-3.0.2 / print-sl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-22  |  5.7 KB  |  245 lines

  1. /*
  2.  * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1995
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #ifndef lint
  23. static  char rcsid[] =
  24.     "@(#)$Header: print-sl.c,v 1.28+ 94/06/10 17:01:38 mccanne Exp $ (LBL)";
  25. #endif
  26.  
  27. #ifdef CSLIP
  28. #include <sys/param.h>
  29. #include <sys/time.h>
  30. #include <sys/timeb.h>
  31. #include <sys/file.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/mbuf.h>
  34. #include <sys/socket.h>
  35.  
  36. #include <net/if.h>
  37. #include <netinet/in.h>
  38. #include <netinet/in_systm.h>
  39. #include <netinet/ip.h>
  40. #include <netinet/if_ether.h>
  41. #include <netinet/ip_var.h>
  42. #include <netinet/udp.h>
  43. #include <netinet/udp_var.h>
  44. #include <netinet/tcp.h>
  45. #include <netinet/tcpip.h>
  46.  
  47. #include <net/slcompress.h>
  48. #include <net/slip.h>
  49.  
  50. #include <ctype.h>
  51. #include <netdb.h>
  52. #include <pcap.h>
  53. #include <signal.h>
  54. #include <stdio.h>
  55.  
  56. #include "interface.h"
  57. #include "addrtoname.h"
  58.  
  59. static int lastlen[2][256];
  60. static int lastconn = 255;
  61.  
  62. static void sliplink_print(const u_char *, const struct ip *, int);
  63. static void compressed_sl_print(const u_char *, const struct ip *, int, int);
  64.  
  65. void
  66. sl_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
  67. {
  68.     register int caplen = h->caplen;
  69.     register int length = h->len;
  70.     register const struct ip *ip;
  71.  
  72.     ts_print(&h->ts);
  73.  
  74.     if (caplen < SLIP_HDRLEN) {
  75.         printf("[|slip]");
  76.         goto out;
  77.     }
  78.     /*
  79.      * Some printers want to get back at the link level addresses,
  80.      * and/or check that they're not walking off the end of the packet.
  81.      * Rather than pass them all the way down, we set these globals.
  82.      */
  83.     packetp = p;
  84.     snapend = p + caplen;
  85.  
  86.     length -= SLIP_HDRLEN;
  87.  
  88.     ip = (struct ip *)(p + SLIP_HDRLEN);
  89.  
  90.     if (eflag)
  91.         sliplink_print(p, ip, length);
  92.  
  93.     ip_print((u_char *)ip, length);
  94.  
  95.     if (xflag)
  96.         default_print((u_char *)ip, caplen - SLIP_HDRLEN);
  97.  out:
  98.     putchar('\n');
  99. }
  100.  
  101. static void
  102. sliplink_print(register const u_char *p, register const struct ip *ip,
  103.            register int length)
  104. {
  105.     int dir;
  106.     int hlen;
  107.  
  108.     dir = p[SLX_DIR];
  109.     putchar(dir == SLIPDIR_IN ? 'I' : 'O');
  110.     putchar(' ');
  111.  
  112.     if (nflag) {
  113.         /* XXX just dump the header */
  114.         int i;
  115.  
  116.         for (i = 0; i < 15; ++i)
  117.             printf("%02x.", p[SLX_CHDR + i]);
  118.         printf("%02x: ", p[SLX_CHDR + 15]);
  119.         return;
  120.     }
  121.     switch (p[SLX_CHDR] & 0xf0) {
  122.  
  123.     case TYPE_IP:
  124.         printf("ip %d: ", length + SLIP_HDRLEN);
  125.         break;
  126.  
  127.     case TYPE_UNCOMPRESSED_TCP:
  128.         /*
  129.          * The connection id is stored in the IP protcol field.
  130.          * Get it from the link layer since sl_uncompress_tcp()
  131.          * has restored the IP header copy to IPPROTO_TCP.
  132.          */
  133.         lastconn = ((struct ip *)&p[SLX_CHDR])->ip_p;
  134.         hlen = ip->ip_hl;
  135.         hlen += ((struct tcphdr *)&((int *)ip)[hlen])->th_off;
  136.         lastlen[dir][lastconn] = length - (hlen << 2);
  137.         printf("utcp %d: ", lastconn);
  138.         break;
  139.  
  140.     default:
  141.         if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
  142.             compressed_sl_print(&p[SLX_CHDR], ip,
  143.                 length, dir);
  144.             printf(": ");
  145.         } else
  146.             printf("slip-%d!: ", p[SLX_CHDR]);
  147.     }
  148. }
  149.  
  150. static const u_char *
  151. print_sl_change(const char *str, register const u_char *cp)
  152. {
  153.     register u_int i;
  154.  
  155.     if ((i = *cp++) == 0) {
  156.         i = (cp[0] << 8) | cp[1];
  157.         cp += 2;
  158.     }
  159.     printf(" %s%d", str, i);
  160.     return (cp);
  161. }
  162.  
  163. static const u_char *
  164. print_sl_winchange(register const u_char *cp)
  165. {
  166.     register short i;
  167.  
  168.     if ((i = *cp++) == 0) {
  169.         i = (cp[0] << 8) | cp[1];
  170.         cp += 2;
  171.     }
  172.     if (i >= 0)
  173.         printf(" W+%d", i);
  174.     else
  175.         printf(" W%d", i);
  176.     return (cp);
  177. }
  178.  
  179. static void
  180. compressed_sl_print(const u_char *chdr, const struct ip *ip,
  181.             int length, int dir)
  182. {
  183.     register const u_char *cp = chdr;
  184.     register u_int flags;
  185.     int hlen;
  186.  
  187.     flags = *cp++;
  188.     if (flags & NEW_C) {
  189.         lastconn = *cp++;
  190.         printf("ctcp %d", lastconn);
  191.     } else
  192.         printf("ctcp *");
  193.  
  194.     /* skip tcp checksum */
  195.     cp += 2;
  196.  
  197.     switch (flags & SPECIALS_MASK) {
  198.     case SPECIAL_I:
  199.         printf(" *SA+%d", lastlen[dir][lastconn]);
  200.         break;
  201.  
  202.     case SPECIAL_D:
  203.         printf(" *S+%d", lastlen[dir][lastconn]);
  204.         break;
  205.  
  206.     default:
  207.         if (flags & NEW_U)
  208.             cp = print_sl_change("U=", cp);
  209.         if (flags & NEW_W)
  210.             cp = print_sl_winchange(cp);
  211.         if (flags & NEW_A)
  212.             cp = print_sl_change("A+", cp);
  213.         if (flags & NEW_S)
  214.             cp = print_sl_change("S+", cp);
  215.         break;
  216.     }
  217.     if (flags & NEW_I)
  218.         cp = print_sl_change("I+", cp);
  219.  
  220.     /*
  221.      * 'hlen' is the length of the uncompressed TCP/IP header (in words).
  222.      * 'cp - chdr' is the length of the compressed header.
  223.      * 'length - hlen' is the amount of data in the packet.
  224.      */
  225.     hlen = ip->ip_hl;
  226.     hlen += ((struct tcphdr *)&((int32 *)ip)[hlen])->th_off;
  227.     lastlen[dir][lastconn] = length - (hlen << 2);
  228.     printf(" %d (%d)", lastlen[dir][lastconn], cp - chdr);
  229. }
  230. #else
  231. #include <sys/types.h>
  232. #include <sys/time.h>
  233.  
  234. #include <stdio.h>
  235.  
  236. #include "interface.h"
  237. void
  238. sl_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
  239. {
  240.  
  241.     error("not configured for slip");
  242.     /* NOTREACHED */
  243. }
  244. #endif
  245.